%% -*- coding: utf-8 -*-

%% @doc てんぷら宣言処理のテスト
%%
-module(t_templ_decl).
-import(templ, [init/0,init/1,process/1,process/2,process/3,process/4]).
-import(templ_decl, [parse/1]).

-include_lib("eunit/include/eunit.hrl").

parse_test_() ->
	[?_assertMatch({[],[]}, 
				   parse("{*!*}")),
	 ?_assertMatch({[],[]},
				   parse("{*! 	*}   \n")),
	 ?_assertMatch({[],"asdf\nfoo\nbar"},
				   parse("{*!*}\nasdf\nfoo\nbar")),

	 ?_assertMatch(none,
				   parse("")),
	 ?_assertMatch(none,
				   parse("asdf\n")),
	 ?_assertMatch(none,
				   parse(" {*!*}")),

	 ?_assertMatch({[{version,"0.8"}],[]},
				   parse("{*! version=\"0.8\" *}")),
	 ?_assertMatch({[{rdelm, ")#"}, {ldelm, "#("}, {version,"0.8"}],[]},
				   parse("{*! version=\"0.8\" ldelm=\"#(\" rdelm=\")#\" *}")),
	 ?_assertMatch({[{rdelm, ")#"}, {ldelm, "#("}, {version,"0.8"}],[]},
				   parse("{*!  version =   \"0.8\"     ldelm=\"#(\"  rdelm=\")#\"*}")),

	 ?_assertThrow({{templ_decl,einval},{4,"}"}},
				   parse("{*!}")),
	 ?_assertThrow({{templ_decl,einval},{6,"hoge"}},
				   parse("{*!*}hoge")),
	 ?_assertThrow({{templ_decl,einval},{7,"*}"}},
				   parse("{*!foo*}")),
	 ?_assertThrow({{templ_decl,einval},{8,"*}"}},
				   parse("{*!foo=*}")),
	 ?_assertThrow({{templ_decl,einval},{11,[]}},
				   parse("{*!foo=\"*}")),
	 ?_assertThrow({{templ_decl,einval},{4,"\"foo\"*}"}},
				   parse("{*!\"foo\"*}")),
	 ?_assertThrow({{templ_decl,einval},{29,"rdelm=\")#\" *}"}},
				   parse("{*! version=\"0.8\" ldelm=\"#(\"rdelm=\")#\" *}"))
	].

process_test_() ->
	init([{"title","My Homepage"}]),

	[?_assertMatch("Welcome to My Homepage", 
				   process("{*! ldelm=\"#(\" rdelm=\")#\"*}\nWelcome to #(title)#")),
	 ?_assertMatch("Welcome to My Homepage", 
				   process("{*! ldelm=\"{\" rdelm=\"}\"*}\nWelcome to {title}"))
	].
